using System;
using CodeGenUILayer;
using DesignPatternDefinitions;
using System.Windows.Forms;

namespace CodeGenerators
{
	public abstract class AbstractCodeGenerator : IDPACodeGenerator
	{
		private IDPAProjectGenerator   mProjectGenerator = null;
		private IDPAComponentGenerator mComponentGenerator = null;
		private IDPAClassGenerator     mClassGenerator = null;

		#region Generic Implementation
		private void Generate(string language,IProject project)
		{
			mProjectGenerator = LanguageFactory.CreateProjectGenerator(language);
			mComponentGenerator = LanguageFactory.CreateComponentGenerator(language);
			mClassGenerator = LanguageFactory.CreateClassGenerator(language);

			CodeGenInfoForm form = new CodeGenInfoForm(language);
			if( form.ShowDialog () == DialogResult.Cancel )
			{
				MessageBox.Show("Code Generation aborted by user",":(");
				return;
			}

			string path = form.Path;

			if( mProjectGenerator != null )
				path = mProjectGenerator.GenerateProjectFile( path , project );

			foreach(IProjectComponent component in project.Components) 
			{
				string componentDir = path;
				if( mComponentGenerator != null )
					componentDir = mComponentGenerator.GenerateComponent(path,component);
				foreach( IClass classObj in component.Classes )
				{
					if( mClassGenerator != null )
						mClassGenerator.GenerateClass( componentDir , classObj , component );
				}
			}
		}
		#endregion
		
		#region IDPACodeGenerator Members

		public void GenerateCode(IProject i)
		{
			this.Generate( this.Language , i );
		}

		#endregion

		protected abstract string Language
		{
			get;
		}

        public abstract string Description
        {
            get;
        }
	}
}
